package org.example.mpdemo1.service;

import org.example.mpdemo1.entity.InstruRes;
import org.example.mpdemo1.entity.Instrument;
import org.example.mpdemo1.entity.Reservation;
import org.example.mpdemo1.mapper.InstrumentMapper;
import org.example.mpdemo1.mapper.ReservationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.time.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ReservationService {

    @Autowired
    private ReservationMapper reservationMapper;

    @Autowired
    private InstrumentMapper instrumentMapper;

    @Transactional
    public Map<String, Object> addReservation(Reservation reservation){

        Map<String, Object> map = new HashMap<>();
        List<Reservation> resList = reservationMapper.selectByInstrumentIdAndStatus(reservation.getInstrumentId(), 1);
        for (Reservation res : resList) {
            if (res.getStartTime().getTime() <= reservation.getEndTime().getTime() && res.getEndTime().getTime() >= reservation.getEndTime().getTime()) {
                map.put("timeMsg", "预约失败，该时间段已被预约");
            }
            else if (res.getStartTime().getTime() <= reservation.getStartTime().getTime() && res.getEndTime().getTime() >= reservation.getStartTime().getTime()) {
                map.put("timeMsg", "预约失败，该时间段已被预约");
            }
            else if (res.getStartTime().getTime() >= reservation.getStartTime().getTime() && res.getEndTime().getTime() <= reservation.getEndTime().getTime()) {
                map.put("timeMsg", "预约失败，该时间段已被预约");
            }
        }
        //if (instrumentMapper.selectById(reservation.getInstrumentId()).getStatus() == 1) {
        //   map.put("statusMsg", "预约失败，该器材已被禁用");
        //}
        if (instrumentMapper.selectById(reservation.getInstrumentId())==null) {
            System.out.println(reservation.getInstrumentId());
            map.put("statusMsg", "预约失败，该器材不存在");
        }
        if (map == null || map.isEmpty()) {
            reservationMapper.insertReservation(reservation);
        }
        return map;
    }

    public List<Reservation> getReservationByInstrumentId(int instrumentId) {
        return reservationMapper.selectByInstrumentId(instrumentId);
    }

    public List<Reservation> getReservationByUserId(int userId) {
        return reservationMapper.selectByUserId(userId);
    }

    public List<Reservation> getAllReservation() {
        return reservationMapper.selectAll();
    }

    public void updateStatus(int id, int status) {
        reservationMapper.updateStatus(id, status);
    }

    public List<InstruRes> getAllInstruRes() {
        List<Instrument> instrumentList = instrumentMapper.getAll();
        List<InstruRes> instruResList = new ArrayList<>();
        for (Instrument instrument : instrumentList) {
            InstruRes instruRes = new InstruRes();
            instruRes.setInstrumentId(instrument.getId());
            instruRes.setInstrumentName(instrument.getName());
            instruRes.setInstrumentType(instrument.getType());
            instruRes.setInstrumentIp(instrument.getIp());

            instruRes.setInstrumentState("可预约");

            LocalDate today = LocalDate.now();
            LocalDateTime todayAtSix = today.atTime(6, 0);
            ZonedDateTime zonedDateTime = todayAtSix.atZone(ZoneId.systemDefault());
            System.out.println(ZoneId.systemDefault());
            Timestamp timestamp = Timestamp.from(zonedDateTime.toInstant());

            instruRes.setStartTime("6:00:00");
            instruRes.setStartTimeStamp(timestamp);

            LocalDateTime todayAt12 = today.atTime(12, 0);
            zonedDateTime = todayAt12.atZone(ZoneId.systemDefault());
            timestamp = Timestamp.from(zonedDateTime.toInstant());

            instruRes.setEndTime("12:00:00");
            instruRes.setEndTimeStamp(timestamp);
            //instruRes.setStartTimeStamp(Timestamp.from(instruRes.getStartTimeStamp().toInstant().minus(Duration.ofHours(22))));
            //instruRes.setEndTimeStamp(Timestamp.from(instruRes.getEndTimeStamp().toInstant().minus(Duration.ofHours(22))));

            List<Reservation> resList = reservationMapper.selectByInstrumentIdAndStatus(instrument.getId(), 1);
            for (Reservation res : resList) {
                System.out.println("上午");
                System.out.println(res.getStartTime());
                System.out.println(instruRes.getStartTimeStamp());
                if (res.getStartTime().equals(Timestamp.from(instruRes.getStartTimeStamp().toInstant().minus(Duration.ofHours(8))))){
                    instruRes.setInstrumentState("不可预约");
                }
            }
            instruResList.add(instruRes);

            InstruRes instruRes1 = new InstruRes();
            instruRes1.setInstrumentId(instrument.getId());
            instruRes1.setInstrumentName(instrument.getName());
            instruRes1.setInstrumentType(instrument.getType());
            instruRes1.setInstrumentIp(instrument.getIp());
            instruRes1.setInstrumentState("可预约");

            LocalDateTime todayAt12_0_1 = today.atTime(12, 0, 1);
            zonedDateTime = todayAt12_0_1.atZone(ZoneId.systemDefault());
            timestamp = Timestamp.from(zonedDateTime.toInstant());
            instruRes1.setStartTime("12:00:01");
            instruRes1.setStartTimeStamp(timestamp);
            //instruRes1.setStartTimeStamp(Timestamp.from(instruRes1.getStartTimeStamp().toInstant().minus(Duration.ofHours(22))));


            LocalDateTime todayAt18 = today.atTime(18, 0);
            zonedDateTime = todayAt18.atZone(ZoneId.systemDefault());
            timestamp = Timestamp.from(zonedDateTime.toInstant());

            instruRes1.setEndTime("18:00:00");
            instruRes1.setEndTimeStamp(timestamp);
            //instruRes1.setEndTimeStamp(Timestamp.from(instruRes1.getEndTimeStamp().toInstant().minus(Duration.ofHours(8))));

            for (Reservation res : resList) {
                System.out.println("下午");
                System.out.println(instruRes1.getInstrumentId());
                System.out.println(res.getStartTime());
                System.out.println(instruRes1.getStartTimeStamp());
                if (res.getStartTime().equals(Timestamp.from(instruRes1.getStartTimeStamp().toInstant().minus(Duration.ofHours(8))))){
                    instruRes1.setInstrumentState("不可预约");
                }
            }

            instruResList.add(instruRes1);
        }
        return instruResList;
    }

    public void deleteReservation(int id) {
        reservationMapper.deleteById(id);
    }

}
